package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.AddCourseDTO;
import com.woniu.mapper.DescriptionMapper;
import com.woniu.pojo.Course;
import com.woniu.mapper.CourseMapper;
import com.woniu.pojo.Description;
import com.woniu.pojo.Video;
import com.woniu.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.service.IDescriptionService;
import com.woniu.service.IVideoService;
import com.woniu.vo.CoursePublishVO;
import com.woniu.vo.NewCourseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xiaoruan
 * @since 2023-03-15
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Resource
    CourseMapper courseMapper;
/*    @Resource
    DescriptionMapper descriptionMapper;*/

    @Autowired
    IDescriptionService descriptionService;

    @Autowired
    ICourseService courseService;

    @Autowired
    IVideoService videoService;

    @Value("${params.index.detailCourseNum}")
    private Integer detailCourseNum;

    @Autowired
    TemplateEngine templateEngine;


    // 开启事务的注解
    @Transactional
    @Override
    public String addcourse(AddCourseDTO addCourseDTO) {

        Course course = new Course();
        BeanUtils.copyProperties(addCourseDTO, course);
        this.saveOrUpdate(course);

        Description description = new Description();
        description.setId(course.getId());
        description.setDescription(addCourseDTO.getDescription());

        descriptionService.saveOrUpdate(description);


        return course.getId();


    }

    @Override
    public AddCourseDTO selectById(String courseId) {
        AddCourseDTO addCourseDTO = courseMapper.selectCourseDTOById(courseId);
        return addCourseDTO;
    }

    @Override
    public void updateStatus(String courseId) {
        courseMapper.updateStatus(courseId);
    }

    @Override
    public CoursePublishVO selectPublishVO(String courseId) {

        CoursePublishVO coursePublishVO = courseMapper.selectPublishVO(courseId);

        return coursePublishVO;


    }

    @Override
    public List<NewCourseVO> queryNewCourse(Integer newCourseNum) {

        return  courseMapper.queryNewCourse(newCourseNum);
    }

    @Override
    public List<NewCourseVO> queryHotCourse(Integer hotCourseNum) {
        return  courseMapper.queryHotCourse(hotCourseNum);
    }

    @Override
    public List<NewCourseVO> listbySubjectId(Integer currentPage, String subjectId) {
        if (currentPage == null){
            currentPage=1;
        }
        IPage<NewCourseVO> page = new Page<>(currentPage,'4');
        return courseMapper.listbySubjectId(page,subjectId).getRecords();
    }

    @Override
    public NewCourseVO queryCourseDetilBycourseId(String courseId) {

        return courseMapper.queryCourseDetilBycourseId(courseId);
    }


    @Override
    public List<NewCourseVO> queryDetailCourse(Integer detailCourseNum) {


        return courseMapper.queryDetailCourse(detailCourseNum);

    }



    @Override
    public void createHtml() throws IOException {

        List<Course> courses = courseMapper.selectList(null);

        for (Course course : courses) {

            //1、查询课程简介数据
            Description courseDescription = descriptionService.getById(course.getId());
            //2、查询课程下所有的视频
            List<Video>  videos= videoService.getVideosByCourseId(course.getId());
            //3、查询课程数据
            NewCourseVO courseVO = courseService.queryCourseDetilBycourseId(course.getId());
            //4、查询精选好课
            List<NewCourseVO>  detailCourseVOList = courseService.queryDetailCourse(detailCourseNum);

            //1、创建上下文对象
            Context context =new Context();
            //2、将查询出来的数据设置到该上下文对象中
//        单独一个数据用这个 context.setVariable("courseVO",courseVO);  多个用map
            Map<String, Object> params = new HashMap<>();
            params.put("courseDescription",courseDescription);
            params.put("videos",videos);
            params.put("courseVO",courseVO);
            params.put("detailCourseVOList",detailCourseVOList);
            context.setVariables(params);
            //3、指定静态页面生成的位置
            File file =new File("C:\\Users\\du'du\\Desktop\\one\\3jieduanfengge\\me\\houduan\\wnxt_parent2\\wnxt_user\\src\\main\\resources\\static\\"+
                    "course_detail_"+course.getId()+".html");
            if (!file.exists()){
                file.createNewFile();
            }
            //  4、创建一个输出流对象
            PrintWriter writer = new PrintWriter(file);
            // 5、通过上下文对象，使用输出流生成静态页面
            templateEngine.process("course_detail",context,writer);


        }




    }



}
